package com.wjs.juc.Atmoic;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicMarkableReference;

public class AtmoicMarkAbleDemo {
    static AtomicMarkableReference<Integer> reference=new AtomicMarkableReference<>(100,false);
    public static void main(String[] args) {
        new Thread(()->{
            boolean marked = reference.isMarked();
            System.out.println(Thread.currentThread().getName()+"默认状态："+marked);
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            reference.compareAndSet(100,101,marked,!marked);
        },"t1").start();


        new Thread(()->{
            boolean marked = reference.isMarked();
            System.out.println(Thread.currentThread().getName()+"默认状态："+marked);
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(reference.compareAndSet(101,100,!marked,marked));
        },"t2").start();
    }
}
